package com.suse.common.util.cos;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.StorageClass;
import com.qcloud.cos.region.Region;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

public class CosUtil {

    private static final String SECRET_ID = "AKIDpaK2RvIeBvb5PWTGm9HF0Xue9c4NBxYy";

    private static final String SECRET_KEY = "ufIv23tfMzfEj3FLOJ5raFi2GTehouqa";

    private static final String BUCKET = "emoc-1313215913";

    private  static final String REGION = "ap-chengdu";

    // https://emoc-1313215913.cos.ap-chengdu.myqcloud.com/
    private static final String BASE_URL = "https://" + BUCKET + ".cos." + REGION + ".myqcloud.com/";

    private static COSClient getCOSClient() {
        // 1 初始化用户身份信息
        COSCredentials cred = new BasicCOSCredentials(SECRET_ID, SECRET_KEY);
        // 2 设置地域
        Region region = new Region(REGION);
        ClientConfig clientConfig = new ClientConfig(region);
        // 3 设置使用 https 协议
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 4 生成 COSClient 客户端
        return new COSClient(cred, clientConfig);
    }


    public static String upload(MultipartFile file) {
        // 1 获取 COSClient 对象
        COSClient cosClient = getCOSClient();
        // 2 指定文件将要存放的存储桶
        // 3 指定文件上传到 COS 上的路径，即对象键。
        String date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
        String uuid = UUID.randomUUID().toString().replace("-","");
        String key = date + "/" + uuid + ".jpg";
        // 4 设置元数据信息
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(file.getSize());
        objectMetadata.setContentType(file.getContentType());
        try {
            InputStream inputStream = file.getInputStream();
            PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET, key, inputStream, objectMetadata);
            putObjectRequest.setStorageClass(StorageClass.Standard);
            cosClient.putObject(putObjectRequest);
            // 6 关闭资源
            // cosClient.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 	https://emoc-1313215913.cos.ap-chengdu.myqcloud.com/2022/08/05/3a5b819609194eb685d3464a3306db7e.jpg
        return BASE_URL + key;
    }

    public static boolean delete(String url) {
        try {
            COSClient cosClient = getCOSClient();
            String key = url.replace(BASE_URL, "");
            cosClient.deleteObject(BUCKET,key);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static void deleteBatch(String[] urls) {
        COSClient cosClient = getCOSClient();
        for (String url : urls) {
            String key = url.replace(BASE_URL, "");
            cosClient.deleteObject(BUCKET,key);
        }
    }


}
